﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using H2.NET.Business.Core;
using H2.NET.Web.Workflow.Services;
using H2.NET.Web.Workflow.DwzCode;
using H2.NET.Web.Workflow.Helpers;
using System.Text;
using H2.NET.Common;

namespace H2.NET.Web.Management.Account
{
    public partial class Edit : BasePage
    {
        public t_mdc_Account account = new t_mdc_Account();

        private readonly AccountService _accountService = new AccountService();
        private readonly RoleService _roleService = new RoleService();

        protected void Page_Load(object sender, EventArgs e)
        {
            var action = Request.QueryString["action"];

            if (action == "save")
            {
                DoEdit();
            }
            else
            {
                InitData();
            }
        }

        private void InitData()
        {
            var id = Request.QueryString["id"];

            account = _accountService.GetModel(Convert.ToInt32(id));

            var roles = ConvertHelper<t_mdc_Role>.ConvertToList(_roleService.GetList("").Tables[0]);
            var userRoles = _accountService.GetRolesByUserId(Convert.ToInt32(id));

            var options = new StringBuilder();
            foreach (var role in roles)
            {
                if (userRoles.Select(t => t.Id).Contains(role.Id))
                {
                    options.AppendLine("<option value='" + role.Id + "' selected>" + role.Name + "</option>");
                }
                else
                {
                    options.AppendLine("<option value='" + role.Id + "'>" + role.Name + "</option>");
                }
            }
            l_options.Text = options.ToString();
        }

        private void DoEdit()
        {
            var id = Request.Form["Id"];
            var username = Request.Form["Username"];
            var password = Request.Form["newPassword"];

            if (Request.Form["Username"] == "*")
            {
                ErrorMsg("对不起，系统默认账号名称不能修改！");
                return;
            }

            var user = new t_mdc_Account()
            {
                Id = AksRequest.GetFormInt("id"),
                Username = AksRequest.GetFormString("Username"),
                Password = Util.Encrypt(AksRequest.GetFormString("newPassword")),
                LastLoginTime = Convert.ToDateTime(AksRequest.GetFormString("LastLoginTime")),
                LastLoginIp = AksRequest.GetFormString("LastLoginIp"),
                Status = Convert.ToBoolean(AksRequest.GetFormString("Status"))

            };

            if (_accountService.ExistsByName(user.Username, AksRequest.GetFormString("OldUsername")))
            {
                ErrorMsg("该用户名已存在！");
                return;
            }

            _accountService.Update(user);

            var roles = Request.Form["roles"] ?? "";

            _roleService.SetRoles(user.Id, roles.Split(',').ToArray());

            SuccessMsg("AccountIndex");
        }
    }
}